#pragma once
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string>
#include"common.hpp"

class inetAddr
{
private:
    struct sockaddr_in _net_addr;
    std::string _ip;
    uint16_t _port;

    void portNet2Host()
    {
        _port = ::ntohs(_net_addr.sin_port);
    }
    void ipNte2Host()
    {
        char ipbuffer[1024];
        const char* ip = ::inet_ntop(AF_INET, &_net_addr.sin_addr, ipbuffer, sizeof(ipbuffer));
        (void)ip;
    }
public:
    inetAddr(/* args */)
    {

    }
    inetAddr(const struct sockaddr_in & addr) :_net_addr(addr)
    {
        portNet2Host();
        ipNte2Host();
    }
    inetAddr(uint16_t port):_port(port)
    {
        _net_addr.sin_family = AF_INET;
        _net_addr.sin_port = ::htons(_port);
        _net_addr.sin_addr.s_addr = INADDR_ANY;
    }
    struct sockaddr*  netAdddr()
    {
        return conv(&_net_addr);
    }
    size_t netAddrLen()
    {
        return sizeof(_net_addr);
    }
    ~inetAddr();

    std::string ip()
    {
        return _ip;
    }

    void SetAddr(const sockaddr_in &client, socklen_t &len)
    {
        _net_addr = client;
        (void)len;
        ipNte2Host();
    }
    uint16_t port() {return _port;}
};



inetAddr::~inetAddr()
{
}



